<?php
/**
 * Created by PhpStorm.
 * User: 76683
 * Date: 2018/7/21
 * Time: 8:15
 */

namespace app\api\model;


use think\Model;

class WeAds extends Model
{
    protected $autoWriteTimestamp = true;
    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function dayAds()
    {
        $time = time();
        $map['is_delete'] = 0;
        $map['start_time'] = ['<=', $time];
        $map['end_time'] = ['>', $time];
        $data_list = self::where(array_merge($map,['is_top'=>['<>','1']]))->field('name,app_id,page,banner,type')->select();
        $top_ad = self::where(array_merge($map,['is_top'=>1]))->field('name,app_id,page,banner,type')->find();
        if (empty($top_ad)){
            $top_ad = [];
        }else{
            $top_ad = $top_ad->toArray();
        }
        if (empty($data_list)){
            $data_list =  [];
            $data_list['top_ad'] = $top_ad;
        }else{
            $data_list = collection($data_list)->toArray();
            $data_list['top_ad'] = $top_ad;
        }
        return $data_list;
    }

    public function getBannerAttr($value){
        return config('web_site_domain').get_file_path($value);
    }
}